package com.atguigu.spzx.manager.controller;


import com.atguigu.spzx.manager.service.CategoryBrandService;
import com.atguigu.spzx.model.dto.product.CategoryBrandDto;
import com.atguigu.spzx.model.entity.product.Brand;
import com.atguigu.spzx.model.entity.product.CategoryBrand;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/product/categoryBrand")
@Tag(name = "分类品牌管理模块")
public class CategoryBrandController {
    @Autowired
    private CategoryBrandService categoryBrandService;

    @GetMapping("/{page}/{limit}")
    @Operation(summary = "分页查询分类品牌管理信息")
    public Result<PageInfo<CategoryBrand>> getGategoryBrandByPage(@PathVariable Integer page,
                                                                  @PathVariable Integer limit,
                                                                  CategoryBrandDto categoryBrandDto) {
        PageInfo<CategoryBrand> list = categoryBrandService.getCategoryBrandByPage(page, limit, categoryBrandDto);
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("save")
    @Operation(summary = "新增分类品牌信息")
    public Result save(@RequestBody CategoryBrand categoryBrand) {
        categoryBrandService.addCategoryBrand(categoryBrand);
        return Result.build(null, ResultCodeEnum.SUCCESS).setMsg("添加成功");
    }

    @PutMapping("updateById")
    @Operation(summary = "根据id修改分类品牌信息")
    public Result updateCategoryBrand(@RequestBody CategoryBrand categoryBrand) {
        categoryBrandService.updateCategoryBrand(categoryBrand);
        return Result.build(null, ResultCodeEnum.SUCCESS).setMsg("保存成功");
    }

    @DeleteMapping("deleteById/{id}")
    @Operation(summary = "根据id删除分类品牌信息")
    public Result deleteById(@PathVariable Long id) {
        categoryBrandService.deleteById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS).setMsg("");
    }
    @GetMapping("FindBrandByCategoryId/{categoryId}")
    @Operation(summary = "根据分类id查询该分类下所有品牌")
    public Result<List<Brand>> FindBrandByCategoryId(@PathVariable Long categoryId){
        List<Brand> brandList= categoryBrandService.getBrandByCategoryId(categoryId);
        return Result.build(brandList,ResultCodeEnum.SUCCESS);
    }
}
